package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketComment;
import com.market.bean.MarketCommentExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketCommentMapper;
import com.market.service.admin.AdminCommentService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wh194@foxmail.com
 * @since 2022/12/30 22:02
 */
@Service
public class AdminCommentServiceImpl implements AdminCommentService {
    @Autowired
    MarketCommentMapper commentMapper;

    /**
     * 评论列表
     *
     * @param info
     * @param userId
     * @param valueId
     * @return
     */
    @Override
    public CommonData<MarketComment> list(BasePageInfo info, String userId, String valueId) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        marketCommentExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        if (!StringUtil.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.valueOf(userId));
        }
        if (!StringUtil.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.valueOf(valueId));
        }
        criteria.andDeletedEqualTo(false);
        List<MarketComment> list = commentMapper.selectByExample(marketCommentExample);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);

        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public void delete(MarketComment comment) {
        Integer id = comment.getId();
        Boolean deleted = comment.getDeleted();
        MarketComment newComment = new MarketComment();
        newComment.setId(id);
        newComment.setDeleted(!deleted);
        commentMapper.updateByPrimaryKeySelective(newComment);
    }
}
